const CompressionWebpackPlugin = require('compression-webpack-plugin')
const path = require('path')
function resolve(dir) {
    return path.join(__dirname, dir)
}
module.exports = {
    chainWebpack: config => {
        // 移除 prefetch 插件
        config.plugins.delete('prefetch')
        config.module
            .rule('svg')
            .exclude.add(resolve('src/icons'))
            .end()
        config.module
            .rule('icons')
            .test(/\.svg$/)
            .include.add(resolve('src/icons'))
            .end()
            .use('svg-sprite-loader')
            .loader('svg-sprite-loader')
            .options({
                symbolId: 'icon-[name]'
            })
            .end()
    },
    configureWebpack: config => {
      const  alias = {
            '@': resolve('src'),
            '_c': resolve('src/components'),
            '_v': resolve('src/pages')
        }
        config.resolve.alias = {
            '@': resolve('src'),
            '_c': resolve('src/components'),
            '_v': resolve('src/pages')
        }
        // Object.assign( config.resolve.alias,alias)

        //生产环境打包设置
        if (process.env.NODE_ENV === 'production') {
            let plugins = [
                new CompressionWebpackPlugin({
                    filename: '[path].gz[query]',
                    algorithm: 'gzip',
                    test: new RegExp(
                        '\\.(' +
                        ['js', 'css'].join('|') +
                        ')$',
                    ),
                    threshold: 51200,
                    minRatio: 0.8,
                })
            ]
            config.plugins = [...config.plugins, ...plugins]
            // 将每个依赖包打包成单独的js文件
            let optimization = {
                // runtimeChunk: 'single',
                // splitChunks: {
                //     chunks: 'all',
                //     maxInitialRequests: Infinity,
                //     minSize: 20000,
                //     cacheGroups: {
                //         vendor: {
                //             test: /[\\/]node_modules[\\/]/,
                //             name(module) {
                //                 // get the name. E.g. node_modules/packageName/not/this/part.js
                //                 // or node_modules/packageName
                //                 const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
                //                 // npm package names are URL-safe, but some servers don't like @ symbols
                //                 return `yxs.${packageName.replace('@', '')}`
                //             }
                //         }
                //     }
                // }
            }
            Object.assign(config, { optimization })
        }
    },
    publicPath: '/',
    productionSourceMap: false,// 生产环境是否生成 sourceMap 文件
    devServer: {
        open: true, // 自动打开浏览器
        disableHostCheck: false,
        host: '0.0.0.0', // 监听的IP
        port: 88, // 启动端口号
        https: false,
        hotOnly: false,
        compress: true, // gzip
        proxy: {
            '/tianqi': {
                target: 'https://api.seniverse.com/',
                ws: false, //proxy websocket
                changeOrigin: true,
                pathRewrite: {
                    '^/tianqi': '', //重写之后url为 http://192.168.1.16:8085/xxxx
                    // '^/api': '/api' //重写之后url为 http://192.168.1.16:8085/api/xxxx
                },
            }
        }, // string | Object
        before: app => { }
    }
}
//打印环境变量
console.log("环境：", process.env.NODE_ENV)
console.log("VUE_APP_BASE_API：", process.env.VUE_APP_BASE_API)
